package com.searchparking.BUS;

import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.List;

import com.searchparking.R;

	public class DirectionListAdapter extends BaseAdapter {

	    List<HashMap<String, String>> listRoutes = null;
	    Context context;
	    String road, distance, duration;
	   
	    public DirectionListAdapter(Context context,List<HashMap<String, String>> listRoutes) {
	        this.context = context;
	        this.listRoutes = listRoutes;
	    }

	    @Override
	    public int getCount() {
	        // TODO Auto-generated method stub
	        return listRoutes.size();
	    }

	    @Override
	    public Object getItem(int pos) {
	        // TODO Auto-generated method stub
	        return listRoutes.get(pos);
	    }

	    @Override
	    public long getItemId(int pos) {
	        // TODO Auto-generated method stub
	        return pos;
	    }

	    @Override
	    public View getView(int pos, View convertView, ViewGroup parent) {
	        // TODO Auto-generated method stub
	        if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) context
						.getSystemService(context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.list_item_direction, null);
			}
             
	        road = listRoutes.get(pos).get("html_instructions").toString();
	        distance = listRoutes.get(pos).get("distance_steps").toString();
	        duration = listRoutes.get(pos).get("duration_steps").toString();
	        ImageView image = (ImageView)convertView.findViewById(R.id.imgIcon);
	        TextView name = (TextView) convertView.findViewById(R.id.txtDirect);
	        TextView distance = (TextView) convertView.findViewById(R.id.txtKhoangcach);
	        
	         int  left = road.indexOf("Turn left");
	         int right = road.indexOf("Turn right");
	         int head = road.indexOf("Head");
	          
	        if(road.contains("left"))
	        {
	        	Log.d("DIRRECT","LEFT");
	        	image.setImageResource(R.drawable.ic_left);
	        }
	        else if(road.contains("right"))
	        {
	        	Log.d("DIRRECT","RIGHT");
	        	image.setImageResource(R.drawable.ic_right);
	        }
	        else if(road.contains("Head"))
	        {
	        	Log.d("DIRRECT","HEAD");
	        	image.setImageResource(R.drawable.ic_play);
	        }
	        name.setText(Html.fromHtml(this.convert(road)));
	        distance.setText(this.distance);
	        

	        return convertView;
	    }
	    public String convert(String str)
	    {
	    	String[] substr = {"Head","Turn right onto","northeast on", "left onto ",};
	    	String[] subrep ={"Đi thằng"," Rẽ phải tại"};
  	    	for(int i=0;i<substr.length;i++)
	    	{
	    		if(str.contains(substr[i]))
		    	{
		    		str=str.replaceAll(substr[i],subrep[i]);
		    	}
	    	}
	    	
	       return str;
	    }
	    public void changeImage(ImageView image,int cases)
	    {
	    		switch (cases)
	    		{
	    		case 1:image.setImageResource(R.drawable.ic_left);
	    				break;
	    		case 2:image.setImageResource(R.drawable.ic_right);
    				break;
	    		case 3:image.setImageResource(R.drawable.ic_play);
    				break;
	    		}
	    		
	    		
	    }
	    	
	    
	    
}
